<template>
  <div class="SHZY-common-layout">
    <div class="SHZY-common-layout-center">
      <el-row class="SHZY-common-search-box" >
        <!-- :gutter="16" -->
        <el-form @submit.native.prevent="">
          <el-col :span="6">
            <el-form-item label="年度">
              <el-input
                v-model="query.year"
                placeholder="年度"
                @keyup.enter.native="search"
                clearable
              />
            </el-form-item>
          </el-col>
          <el-col :span="10">
            <el-form-item>
              <el-button type="primary" icon="el-icon-search" @click="search()"
                >查询</el-button
              >
              <el-button icon="el-icon-refresh-right" @click="reset()"
                >重置</el-button
              >
              <el-button
                type="primary"
                icon="el-icon-search"
                @click="yearinit()"
                >初始化</el-button
              >
            </el-form-item>
          </el-col>
        </el-form>
      </el-row>
      <div class="SHZY-common-layout-main SHZY-flex-main">
        <div class="SHZY-common-head">
          <div>
            <el-button
              type="primary"
              icon="el-icon-plus"
              @click="addOrUpdateHandle()"
              >新增</el-button
            >
          </div>
          <div class="SHZY-common-head-right">
            <el-tooltip effect="dark" content="刷新" placement="top">
              <el-link
                icon="icon-ym icon-ym-Refresh SHZY-common-head-icon"
                :underline="false"
                @click="reset()"
              />
            </el-tooltip>
          </div>
        </div>
        <SHZY-table
          v-loading="listLoading"
          :data="list"
          highlight-current-row
          @sort-change="sortChange"
          @current-change="handleSelectionChange"
          custom-column
        >
          <el-table-column
            label="名称"
            prop="name"
            align="left"
            sortable="custom"
          >
          </el-table-column>
          <el-table-column
            label="年度"
            prop="year"
            align="left"
            sortable="custom"
          >
          </el-table-column>
          <el-table-column
            label="有效标志"
            prop="enabledMark"
            sortable="custom"
            width="100"
            align="center"
          >
            <template slot-scope="scope">
              <el-tag
                :type="scope.row.enabledMark == '有效' ? 'success' : 'danger'"
                disable-transitions
              >
                {{ scope.row.enabledMark == "有效" ? "正常" : "停用" }}</el-tag
              >
            </template>
          </el-table-column>
          <el-table-column label="操作" fixed="right" width="150">
            <template slot-scope="scope">
              <el-button type="text" @click="lookHandle(scope.row.year)"
                >查看</el-button
              >
              <el-button type="text" @click="addOrUpdateHandle(scope.row.id)"
                >编辑</el-button
              >
              <el-button
                type="text"
                @click="handleDel(scope.row.id)"
                class="SHZY-table-delBtn"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </SHZY-table>
        <pagination
          :total="total"
          :page.sync="listQuery.currentPage"
          :limit.sync="listQuery.pageSize"
          @pagination="initData"
        />
      </div>
    </div>
    <SHZY-Form v-if="formVisible" ref="SHZYForm" @refresh="refresh" />
    <Sche-Index
      v-if="ScheIndexVisible"
      ref="ScheIndex"
      @refresh="refresh"
      :defaultDate="defaultDate"
    />
    <ExportBox v-if="exportBoxVisible" ref="ExportBox" @download="download" />
  </div>
</template>
<script>
import restAPI from "@/api/q1/basedata/schedule";
import SHZYForm from "./Form";
import ScheIndex from "./ScheIndex";
export default {
  components: { SHZYForm, ScheIndex },
  data() {
    return {
      query: {
        name: undefined,
        year: undefined,
      },
      list: [],
      defaultDate: "",
      listLoading: true,
      multipleSelection: [],
      selectionyear: undefined,
      total: 0,
      listQuery: {
        currentPage: 1,
        pageSize: 20,
        sort: "desc",
        sidx: "",
      },
      formVisible: false,
      ScheIndexVisible: false,
      exportBoxVisible: false,
      columnList: [
        { prop: "name", label: "名称" },
        { prop: "year", label: "年度" },
        { prop: "enabledMark", label: "有效标志" },
      ],
    };
  },
  computed: {
    menuId() {
      return this.$route.meta.modelId || "";
    },
  },
  created() {
    this.initData();
  },
  methods: {
    sortChange({ column, prop, order }) {
      this.listQuery.sort = order == "ascending" ? "asc" : "desc";
      this.listQuery.sidx = !order ? "" : prop;
      this.initData();
    },
    initData() {
      this.listLoading = true;
      this.selectionyear = undefined;
      let _query = {
        ...this.listQuery,
        ...this.query,
        menuId: this.menuId,
      };
      let query = {};
      for (let key in _query) {
        if (Array.isArray(_query[key])) {
          query[key] = _query[key].join();
        } else {
          query[key] = _query[key];
        }
      }
      restAPI.getList(query).then((res) => {
        console.log(res.data.list);
        this.list = res.data.list;
        this.total = res.data.pagination.total;
        this.listLoading = false;
      });
    },
    handleDel(id) {
      this.$confirm("此操作将永久删除该数据, 是否继续?", "提示", {
        type: "warning",
      })
        .then(() => {
          restAPI.delete(id).then((res) => {
            this.$message({
              type: "success",
              message: res.msg,
              onClose: () => {
                this.initData();
              },
            });
          });
        })
        .catch(() => {});
    },
    handleSelectionChange(val) {
      if (val != null) {
        this.selectionyear = val.year;
      }
    },
    lookHandle(year) {
      var y = new Date().getFullYear();
      var m = new Date().getMonth() + 1;
      var d = new Date().getDate();
      m = m < 10 ? "0" + m : m;
      d = d < 10 ? "0" + d : d;
      if (y == year) {
        this.defaultDate = y + "-" + m + "-" + d;
      } else {
        this.defaultDate = year + "-01-01";
      }
      this.ScheIndexVisible = true;
      this.$nextTick(() => {
        this.$refs.ScheIndex.initData(year);
      });
    },
    addOrUpdateHandle(id) {
      this.formVisible = true;
      this.$nextTick(() => {
        this.$refs.SHZYForm.init(id);
      });
    },
    search() {
      this.listQuery = {
        currentPage: 1,
        pageSize: 20,
        sort: "desc",
        sidx: "",
      };
      this.initData();
    },
    refresh(isrRefresh) {
      this.formVisible = false;
      this.ScheIndexVisible = false;
      if (isrRefresh) this.reset();
    },
    reset() {
      for (let key in this.query) {
        this.query[key] = undefined;
      }
      this.listQuery = {
        currentPage: 1,
        pageSize: 20,
        sort: "desc",
        sidx: "",
      };
      this.initData();
    },
    yearinit() {
      if (!this.selectionyear) {
        this.$message({
          type: "error",
          message: "请选择一条数据",
          duration: 1500,
        });
        return;
      }
      const year = this.selectionyear;

      restAPI.inityear(year).then((res) => {
        this.$message({
          type: "success",
          message: res.msg,
          onClose: () => {
            this.initData();
          },
        });
      });
    },
  },
};
</script>